{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4.6 \u5e26\u6709\u5916\u90e8\u72b6\u6001\u7684\u751f\u6210\u5668\u51fd\u6570\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5b9a\u4e49\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\uff0c\u4f46\u662f\u5b83\u4f1a\u8c03\u7528\u67d0\u4e2a\u4f60\u60f3\u66b4\u9732\u7ed9\u7528\u6237\u4f7f\u7528\u7684\u5916\u90e8\u72b6\u6001\u503c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u8ba9\u4f60\u7684\u751f\u6210\u5668\u66b4\u9732\u5916\u90e8\u72b6\u6001\u7ed9\u7528\u6237\uff0c\n\u522b\u5fd8\u4e86\u4f60\u53ef\u4ee5\u7b80\u5355\u7684\u5c06\u5b83\u5b9e\u73b0\u4e3a\u4e00\u4e2a\u7c7b\uff0c\u7136\u540e\u628a\u751f\u6210\u5668\u51fd\u6570\u653e\u5230 __iter__() \u65b9\u6cd5\u4e2d\u8fc7\u53bb\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from collections import deque\n\nclass linehistory:\n    def __init__(self, lines, histlen=3):\n        self.lines = lines\n        self.history = deque(maxlen=histlen)\n\n    def __iter__(self):\n        for lineno, line in enumerate(self.lines, 1):\n            self.history.append((lineno, line))\n            yield line\n\n    def clear(self):\n        self.history.clear()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u4f7f\u7528\u8fd9\u4e2a\u7c7b\uff0c\u4f60\u53ef\u4ee5\u5c06\u5b83\u5f53\u505a\u662f\u4e00\u4e2a\u666e\u901a\u7684\u751f\u6210\u5668\u51fd\u6570\u3002\n\u7136\u800c\uff0c\u7531\u4e8e\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u4e8e\u662f\u4f60\u53ef\u4ee5\u8bbf\u95ee\u5185\u90e8\u5c5e\u6027\u503c\uff0c\n\u6bd4\u5982 history \u5c5e\u6027\u6216\u8005\u662f clear() \u65b9\u6cd5\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with open('somefile.txt') as f:\n    lines = linehistory(f)\n    for line in lines:\n        if 'python' in line:\n            for lineno, hline in lines.history:\n                print('{}:{}'.format(lineno, hline), end='')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5173\u4e8e\u751f\u6210\u5668\uff0c\u5f88\u5bb9\u6613\u6389\u8fdb\u51fd\u6570\u65e0\u6240\u4e0d\u80fd\u7684\u9677\u9631\u3002\n\u5982\u679c\u751f\u6210\u5668\u51fd\u6570\u9700\u8981\u8ddf\u4f60\u7684\u7a0b\u5e8f\u5176\u4ed6\u90e8\u5206\u6253\u4ea4\u9053\u7684\u8bdd(\u6bd4\u5982\u66b4\u9732\u5c5e\u6027\u503c\uff0c\u5141\u8bb8\u901a\u8fc7\u65b9\u6cd5\u8c03\u7528\u6765\u63a7\u5236\u7b49\u7b49)\uff0c\n\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4f60\u7684\u4ee3\u7801\u5f02\u5e38\u7684\u590d\u6742\u3002\n\u5982\u679c\u662f\u8fd9\u79cd\u60c5\u51b5\u7684\u8bdd\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4e0a\u9762\u4ecb\u7ecd\u7684\u5b9a\u4e49\u7c7b\u7684\u65b9\u5f0f\u3002\n\u5728 __iter__() \u65b9\u6cd5\u4e2d\u5b9a\u4e49\u4f60\u7684\u751f\u6210\u5668\u4e0d\u4f1a\u6539\u53d8\u4f60\u4efb\u4f55\u7684\u7b97\u6cd5\u903b\u8f91\u3002\n\u7531\u4e8e\u5b83\u662f\u7c7b\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u5141\u8bb8\u4f60\u5b9a\u4e49\u5404\u79cd\u5c5e\u6027\u548c\u65b9\u6cd5\u6765\u4f9b\u7528\u6237\u4f7f\u7528\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a\u9700\u8981\u6ce8\u610f\u7684\u5c0f\u5730\u65b9\u662f\uff0c\u5982\u679c\u4f60\u5728\u8fed\u4ee3\u64cd\u4f5c\u65f6\u4e0d\u4f7f\u7528for\u5faa\u73af\u8bed\u53e5\uff0c\u90a3\u4e48\u4f60\u5f97\u5148\u8c03\u7528 iter() \u51fd\u6570\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('somefile.txt')\nlines = linehistory(f)\nnext(lines)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Call iter() first, then start iterating\nit = iter(lines)\nnext(it)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "next(it)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}